/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-06-26     WangJing       the first version
 */
#include "pharmacy.h"
#include <rtthread.h>

#define DBG_TAG "Pharmacy"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

// 删除指定药品
void pharmacy_delete(int pharmacyData[PHARMACY_NUM][MEDICINE_NUM],int pharmacyNum,int medicineNum){
    pharmacyData[pharmacyNum][medicineNum] -= 1;
}

// 药单数据处理({<药房号>:<药1><药2><药3>...})
int pharmacy_data_process(int pharmacyData[PHARMACY_NUM][MEDICINE_NUM],char* dataAddr,int clear) {
    int pharmacy = 0;
    int dateSize = strlen(dataAddr);
    if (dataAddr[0] != '{' || dataAddr[2] != ':' || dataAddr[dateSize-1] != '}')
        return -1; // 未接收到对应格式的数据

    if (dataAddr[1] - '0' >= 0 && dataAddr[1] - '0' < PHARMACY_NUM)
        pharmacy = dataAddr[1] - '0';
    else
        return -2; // 药房数据的格式违法

    for (int i = 3; i < dateSize - 1; i++) {
        if(!(dataAddr[i] >= 'A' && dataAddr[i] < 'A' + MEDICINE_NUM))
            return -3; // 药的数据格式违法
    }

    for (int i = 3; i < dateSize - 1; i++) {
        pharmacyData[pharmacy][dataAddr[i]-'A'] += 1;
    }

    if(clear != 0)
        dataAddr[0] = '\0'; // 读取成功偶清除缓冲区

    return 0;

}

// 显示药单
void pharmacy_prescription_show(int pharmacyData[PHARMACY_NUM][MEDICINE_NUM]) {
    rt_kprintf("==================================================\n  ");
    for (int j = 0; j < MEDICINE_NUM; j++) {
        rt_kprintf(" %c ", 'A' + j);
    }
    rt_kprintf("\n");
    for (int i = 0; i < PHARMACY_NUM; i++) {

        rt_kprintf("%d:", i);
        for (int j = 0; j < MEDICINE_NUM; j++) {
            rt_kprintf(" %d ", pharmacyData[i][j]);
        }
        rt_kprintf("\n");
    }
    rt_kprintf("==================================================\n");
}

